package com.itboye.ihomebank.activity.finance.fragment;

import android.annotation.SuppressLint;
import android.content.Context;
import android.content.Intent;
import android.view.View;
import android.widget.AdapterView;
import android.widget.LinearLayout;
import android.widget.ListView;

import com.itboye.ihomebank.R;
import com.itboye.ihomebank.adapter.HaveInhaAdapter;
import com.itboye.ihomebank.base.BaseFragment;
import com.itboye.ihomebank.base.ptr.BasePtr;
import com.itboye.ihomebank.bean.HaveInBean;
import com.itboye.ihomebank.bean.ResultEntity;
import com.itboye.ihomebank.constants.SPContants;
import com.itboye.ihomebank.activity.finance.MyTouZiActivity;
import com.itboye.ihomebank.presenter.JinRongPresenter;
import com.itboye.ihomebank.util.ByAlert;
import com.itboye.ihomebank.util.DoubleUtil;
import com.itboye.ihomebank.util.SPUtils;
import com.itboye.ihomebank.util.UIAlertView;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.Observable;
import java.util.Observer;

import in.srain.cube.views.ptr.PtrDefaultHandler2;
import in.srain.cube.views.ptr.PtrFrameLayout;

/**
 * 我的投资(回款中)
 */
@SuppressLint("ValidFragment")
public class HuiKuanZhongFrament extends BaseFragment implements Observer ,HaveInhaAdapter.ClickListener{
    private int type;
    private ListView listView;
    private HaveInhaAdapter adapter;
    private HaveInBean list;
    List<HaveInBean.InfoList> lists;
    private JinRongPresenter presenter;
    String uid;
    int pageSize = 10;
    int pageNum = 1;

    PtrFrameLayout commend_anchor_ptr;//刷新
    private LinearLayout linNull, linhuikuan;
    String id;
    int ppp;
    private UIAlertView uiAlertView;
    @Override
    public int initView() {
        return R.layout.fragment_haveinhand;
    }

    public HuiKuanZhongFrament(int type) {
        this.type = type;
    }

    @Override
    public void onMyActivityCreated() {
        BasePtr.setPagedPtrStyle(commend_anchor_ptr);//设置上拉下拉刷新
        list = new HaveInBean();
        lists=new ArrayList<HaveInBean.InfoList>();

        presenter = new JinRongPresenter(this);
        uid = (String) SPUtils.get(getActivity(), null, SPContants.USER_ID, "");
        showProgressDialog("数据加载中...", true);
        presenter.onMyTouZhi(uid, pageNum + "", pageSize + "", type + "");
        adapter = new HaveInhaAdapter(lists, getActivity(),this);
        listView.setAdapter(adapter);
        commend_anchor_ptr.setPtrHandler(new PtrDefaultHandler2() {
            @Override
            public void onLoadMoreBegin(PtrFrameLayout frame) {
                pageNum++;
                presenter.onMyTouZhi(uid, pageNum + "", pageSize + "", type + "");
            }

            @Override
            public void onRefreshBegin(PtrFrameLayout frame) {
                presenter.onMyTouZhi(uid, pageNum + "", pageSize + "", type + "");
            }
        });
        commend_anchor_ptr.autoRefresh();
        listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                Intent Intet = new Intent(getActivity(), MyTouZiActivity.class);
                Intet.putExtra("id", lists.get(position).getId());
                startActivity(Intet);
            }
        });

    }

    @Override
    public void update(Observable observable, Object data) {
        ResultEntity entity = handlerError(data);

        if (entity != null) {
            if (!entity.getCode().equals(0)) {
                if (entity.getEventType() == JinRongPresenter.my_tou_zi_success) {
                    if (pageNum == 1) {
                        lists.clear();
                    }
                    commend_anchor_ptr.refreshComplete();//设置刷新完成
                    list = (HaveInBean) entity.getData();
                    lists.addAll(list.getInfo());
                    if (lists.size() > 0) {
                        linhuikuan.setVisibility(View.GONE);
                        commend_anchor_ptr.setVisibility(View.VISIBLE);
                        adapter.notifyDataSetChanged();
                    }else{
                        linhuikuan.setVisibility(View.VISIBLE);
                        commend_anchor_ptr.setVisibility(View.GONE);
                    }
                }
                if (entity.getEventType() == JinRongPresenter.my_tou_zi_fail) {
                    ByAlert.alert(entity.getData());
                }
                if (entity.getEventType() == JinRongPresenter.zhuanrang_success) {
                    ByAlert.alert(entity.getData());
                    Iterator<HaveInBean.InfoList> iterator=lists.iterator();
                    while (iterator.hasNext()){
                        HaveInBean.InfoList bean=iterator.next();
                        if (bean.getId().equals(id)){
                            //  iterator.remove();
                            lists.get(ppp).setCan_transfer("0");
                            adapter.notifyDataSetChanged();
                            break;
                        }
                    }
                }
                if (entity.getEventType() == JinRongPresenter.zhuanrang_fail) {
                    ByAlert.alert(entity.getData());
                }
            }
            try {
                closeProgressDialog();
            } catch (Exception e) {
            }
        }
    }

    @Override
    public void onCle(final View v, Context context) {
        uiAlertView = new UIAlertView(context, "是否以"+ DoubleUtil.doubleTransform(lists.get((Integer) v.getTag()).getTransfer_principal())+"的价格转让",
                "24小时内未转让成功按原计划回款\n您可以继续发布转让", "确认", "取消");
        uiAlertView.show();
        uiAlertView.setClicklistener(new UIAlertView.ClickListenerInterface() {
            @Override
            public void doLeft() {
                showProgressDialog("请稍后...",true);
                id=lists.get((Integer) v.getTag()).getId();
                ppp=(Integer) v.getTag();
                presenter.fabuZhuangRang(id,uid);
                uiAlertView.dismiss();
            }
            @Override
            public void doRight() {
                uiAlertView.dismiss();
            }
        });
    }
}